package com.example.administrator.dc.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.dc.Activity.Detail;
import com.example.administrator.dc.Bean.Checkcollect;
import com.example.administrator.dc.R;
import com.example.administrator.dc.Bean.School;
import com.example.administrator.dc.Service.CollectListener;
import com.example.administrator.dc.Service.CollectService;
import com.example.administrator.dc.model.CollectModel;
import com.example.administrator.dc.model.Contants;
import com.squareup.picasso.Picasso;

import java.util.List;

;import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class UserListAdapter extends RecyclerView.Adapter<ViewHolder> implements CollectListener {
    private Context mContext;;
    private List mDataList;
    boolean s;
    public LayoutInflater mLayoutInflater;
    public UserListAdapter(Context mContext, List mDataList){
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.inflate(R.layout.cardview_myhome, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final School entity = (School) mDataList.get(position);
        if (null == entity)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.user_info.setText(entity.getTitle());
        viewHolder.user_read.setText(entity.getRead()+"");
       viewHolder.user_collect.setText(entity.getLove());
        SharedPreferences sp = mContext.getSharedPreferences("UserInfo", mContext.MODE_PRIVATE);
        final String userid =sp.getString("user_id", null);
        if(!"".equals(userid)){
            Retrofit retrofit=new Retrofit.Builder().baseUrl(Contants.BASE_URL1).
                    addConverterFactory(GsonConverterFactory.create()).build();
            CollectService service=retrofit.create(CollectService.class);
            Call<Checkcollect> call= service.checkcollect(userid,entity.getBid());
            call.enqueue(new Callback<Checkcollect>() {
                @Override
                public void onResponse(Call<Checkcollect> call, Response<Checkcollect> response) {
                    if(response.body().getStatus()==1){
//                      viewHolder.imageView_collect1.setImageDrawable(mContext.getDrawable(R.drawable.redheart));
                    }
                    else {
          //            viewHolder.imageView_collect1.setImageDrawable(mContext.getDrawable(R.drawable.heart));
                    }

                }

                @Override
                public void onFailure(Call<Checkcollect> call, Throwable t) {

                }
            });


        }



        if (!"".equals(entity.getPhoto()))
        Picasso.with(mContext).load(entity.getPhoto()).into(viewHolder.user_image);
        if(!"1".equals(entity.getGender()+"")){
            viewHolder.user_bottom.setBackgroundResource(R.color.myhome_top_text_color);
            viewHolder.user_info.setTextColor(viewHolder.user_info.getResources().getColor(R.color.myhome_top_text_color));}
        else
        {viewHolder.user_bottom.setBackgroundResource(R.color.pink);
            viewHolder.user_info.setTextColor(viewHolder.user_info.getResources().getColor(R.color.pink));}

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               Intent intent=new Intent(mContext,Detail.class);
                intent.putExtra("address",entity.getAddress());
                mContext.startActivity(intent);

            }
        });

       viewHolder.imageView_collect1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               SharedPreferences sp = mContext.getSharedPreferences("UserInfo", mContext.MODE_PRIVATE);
                final String userid =sp.getString("user_id", null);
                Retrofit retrofit=new Retrofit.Builder().baseUrl(Contants.BASE_URL1).
                        addConverterFactory(GsonConverterFactory.create()).build();
                CollectService service=retrofit.create(CollectService.class);
                Call<Checkcollect> call= service.checkcollect(userid,entity.getBid());
                call.enqueue(new Callback<Checkcollect>() {
                    @Override
                    public void onResponse(Call<Checkcollect> call, Response<Checkcollect> response) {
                        if(response.body().getStatus()==1){
                            CollectModel collectModel=new CollectModel();
                            collectModel.uncollect(userid,entity.getBid(),UserListAdapter.this);
                            viewHolder.imageView_collect1.setImageDrawable(mContext.getDrawable(R.drawable.heart));
                            Toast.makeText(mContext.getApplicationContext(),"取消收藏成功",Toast.LENGTH_SHORT).show();
                        }
                       else {
                            CollectModel collectModel=new CollectModel();
                            collectModel.collect(userid,entity.getBid(),UserListAdapter.this);
                            viewHolder.imageView_collect1.setImageDrawable(mContext.getDrawable(R.drawable.redheart));
                            Toast.makeText(mContext.getApplicationContext(),"收藏成功",Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<Checkcollect> call, Throwable t) {

                    }
                });

            }
        });





    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    @Override
    public void onResponse(String msg) {

    }

    @Override
    public void onFail(String s) {

    }

    @Override
    public void onExitResponse(String msg) {
    }

    @Override
    public void onExitFail(String s) {

    }

    @Override
    public void onFailResponse(String msg) {

    }

    @Override
    public void onResponse1(String msg) {

    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView user_info;
        ImageView user_image;
        TextView user_read;
       TextView user_collect;
        ImageView imageView_collect1;
        RelativeLayout user_bottom;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView_collect1= (ImageView) itemView.findViewById(R.id.imageView_collect);
            user_info = (TextView) itemView.findViewById(R.id.cardview_myhome_info);
            user_image = (ImageView) itemView.findViewById(R.id.cardview_myhome_img);
            user_read = (TextView) itemView.findViewById(R.id.num_myhome_read);
            user_collect= (TextView) itemView.findViewById(R.id.num_myhome_collect);
            user_bottom=(RelativeLayout)itemView.findViewById(R.id.cardview_bottom);

        }
    }






}
